//
//  FQDSwitch.m
//  FakeQDaily
//
//  Created by mewxiv on 9/9/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDSwitch.h"

static NSString *const kWireFrameOnImage = @"Switch-on";
static NSString *const kWireFrameOffImage = @"Switch-off";

@interface FQDSwitch ()

// 开关外框
@property (nonatomic, strong) UIImageView *wireframe;
// 开关小黄点
@property (nonatomic, strong) UIView *dot;

@end

@implementation FQDSwitch

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self prepareUI:frame];
    }
    return self;
}

- (void)prepareUI:(CGRect)frame
{
    _wireframe = [[UIImageView alloc] initWithFrame:frame];
    [self addSubview:_wireframe];
    
    _dot = [[UIView alloc] init];
    _dot.layer.cornerRadius = 6.5f;
    [self addSubview:_dot];
    
    [self updateSwitch];
}

- (void)updateSwitch
{
     if (self.isOn) {
        [_wireframe setImage:[UIImage imageNamed:kWireFrameOnImage]];
        
        CGRect dotFrame = CGRectMake(3.5f, 3.5f, 13.0f, 13.0f);
        _dot.frame = dotFrame;
        _dot.backgroundColor = RGB(255, 200, 31);
    } else {
        [_wireframe setImage:[UIImage imageNamed:kWireFrameOffImage]];
        
        CGRect dotFrame = CGRectMake(3.5f, 3.5f, 13.0f, 13.0f);
        _dot.frame = dotFrame;
        _dot.backgroundColor = RGB(134, 134, 134);
    }
}

- (void)setOn:(BOOL)on
{
    _on = on;
    
    [self updateSwitch];
}

@end
